//
//  RadioViewController.m
//  OurRelax
//
//  Created by 于宾尧 on 15/6/16.
//  Copyright (c) 2015年 于宾尧. All rights reserved.
//

#import "RadioViewController.h"

@interface RadioViewController ()
@property (nonatomic, retain) UITableView *radioTV;
@end

@implementation RadioViewController

-(void)dealloc
{
    [_headerView release];
    [_headerViewArray release];
    [_radioMdlArray release];
    
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self getRadioInfo];
        [self getNewRadioInfo];
        [self getClassicRadioInfo];

        [self subMyViews];
        self.headerViewArray = [NSMutableArray array];
        self.radioMdlArray = [NSMutableArray array];
        self.smallArray = [NSMutableArray array];
        self.bigRadioCell.bigRadioMdlArray = [NSMutableArray array];
        self.radioCellVC.NewRadioMdlArray = [NSMutableArray array];
        self.radioCellVC.classicArray = [NSMutableArray array];
        self.classicArray = [NSMutableArray array];
        self.NewRadioMdlArray = [NSMutableArray array];
        self.playUrlArray = [NSMutableArray array];
    }
    return self;
}

-(void)subMyViews
{
    [self getHeaderViewImage];
    self.headerView = [[HeaderScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
    [self.view addSubview:self.headerView];
    [self.headerView release];
    self.view.backgroundColor = [UIColor clearColor];
    

     self.radioTV = [[UITableView alloc]initWithFrame:CGRectMake(5, 10, self.view.frame.size.width-10, self.view.frame.size.height) style:UITableViewStyleGrouped ];
    self.radioTV.backgroundColor = [UIColor clearColor];

     self.radioTV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 60) style:UITableViewStyleGrouped ];


    _radioTV.delegate = self;
    _radioTV.dataSource = self;
    _radioTV.tableHeaderView = self.headerView;
    [self.view addSubview:_radioTV];
}
-(void)getClassicRadioInfo
{
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    
    NSURL *url = [NSURL URLWithString:@"http://mobile.ximalaya.com/m/explore_album_list?category_name=radio&condition=classic&device=iPhone&page=1&per_page=20&status=0&tag_name="];
    NSString *urlString = [NSString stringWithFormat:@"%@",url];
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];

    
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *array = [dic objectForKey:@"list"];
        
        
        for (NSMutableDictionary *bigRadioDic in array) {
            ClassicsModels *radioMdl = [[ClassicsModels alloc]init];
            [radioMdl setValuesForKeysWithDictionary:bigRadioDic];
            
            [self.classicArray addObject:radioMdl];
            
        }
        
        
        
        
        [self.radioTV reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];
    
}

-(void)getNewRadioInfo
{
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    
    
    NSURL *url = [NSURL URLWithString:@"http://mobile.ximalaya.com/m/explore_album_list?category_name=radio&condition=recent&device=iPhone&page=1&per_page=20&status=0&tag_name="];
    NSString *urlString = [NSString stringWithFormat:@"%@",url];
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *array = [dic objectForKey:@"list"];
        
        
        for (NSMutableDictionary *bigRadioDic in array) {
            bigRadioModels *radioMdl = [[bigRadioModels alloc]init];
            [radioMdl setValuesForKeysWithDictionary:bigRadioDic];
            
            [self.NewRadioMdlArray addObject:radioMdl];
            
        }
        
        
        
        
        [self.radioTV reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
    
    
}


-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"最火";
    }else if (section == 1){
        return @"最近更新";
    }
    return @"经典";
    
}

-(void)getRadioInfo
{
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    
    NSURL *url = [NSURL URLWithString:@"http://mobile.ximalaya.com/m/explore_album_list?category_name=radio&condition=hot&device=iPhone&page=1&per_page=20&status=0&tag_name="];
    NSString *urlString = [NSString stringWithFormat:@"%@",url];
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
//        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *array = [dic objectForKey:@"list"];
        
        
        for (NSMutableDictionary *smallDic in array) {
            RadioModels *radioMdl = [[RadioModels alloc]init];
            [radioMdl setValuesForKeysWithDictionary:smallDic];
            
            [self.radioMdlArray addObject:radioMdl];
        
        }
        
        for (int i = 0; i < 2; i++) {
            RadioModels *smallMdl = [[RadioModels alloc]init];
          smallMdl = [self.radioMdlArray objectAtIndex:i];
            [self.smallArray addObject:smallMdl];
        }
        
        
        [self.radioTV reloadData];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
    
}

-(void)getHeaderViewImage
{
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    
    NSURL *url = [NSURL URLWithString:@"http://online.dongting.com/recomm/recomm_modules?app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPhone7%2C2&f=f320&s=s310&imsi=&hid=&splus=8.3&active=1&net=2&openudid=c5bdf2871397a7699d43e8f620fa201f563e3bd2&idfa=76882D7D-3E10-45D4-B40B-20A0394D00CF&utdid=VXlTA7t9%2BlEDAE055roWXtEe&alf=201200&bundle_id=com.ttpod.music&version=0"];
    NSString *urlString = [NSString stringWithFormat:@"%@",url];
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        
        NSMutableDictionary *bigDic = responseObject;
        NSMutableDictionary *smallDic = [bigDic objectForKey:@"data"];
        NSMutableArray *songDic = [smallDic objectForKey:@"songlists"];
        NSMutableDictionary *ViewDic = [songDic objectAtIndex:0];
        NSMutableArray *dataArr = [ViewDic objectForKey:@"data"];
        for (NSMutableDictionary *dataDic in dataArr) {
            HeaderView *headerModel = [[HeaderView alloc]init];
            [headerModel setValuesForKeysWithDictionary:dataDic];
            
            [self.headerViewArray addObject:headerModel];
        }
        [self.headerView setImages:self.headerViewArray];

        
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];
    
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 130;
    }
    return 150;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 2) {
        return 0;
    }
    return 0;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.smallArray.count;
    }
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.section == 0) {
        
        
        static NSString *cellIndefiner = @"radioCell";
        RadioTableViewCell *radioCell = [tableView dequeueReusableCellWithIdentifier:cellIndefiner];
        if (radioCell == nil) {
            radioCell = [[RadioTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndefiner];
            radioCell.radioMdl = [[RadioModels alloc]init];
            radioCell.radioMdl = [self.smallArray objectAtIndex:indexPath.row];
            
            
            
            
        }
        return radioCell;
    }
    
    static NSString *cellIndefiner = @"radioCell2";
    self.bigRadioCell = [tableView dequeueReusableCellWithIdentifier:cellIndefiner];
    if (self.bigRadioCell == nil) {
        self.bigRadioCell = [[BigRadioCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndefiner];
        
        
        
    }
    

    
    self.radioCellVC = [[RadioCollectionViewController alloc]init];
    
    
    
    [self.bigRadioCell addSubview:self.radioCellVC.view ];
    if (indexPath.section == 1) {
        

        self.radioCellVC.classicArray = self.classicArray;
    }
    if (indexPath.section == 2) {
        self.radioCellVC.classicArray = self.NewRadioMdlArray;
    }

    
    
    return self.bigRadioCell;
    
}

-(void)getRadioPlayingUrl :(RadioModels *)radio

{
    [[Player shareDataHandle].urlArray removeAllObjects];
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    
    NSString *url_string = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%@/true/1/30?device=iPhone",radio.albumId ];

    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        NSMutableDictionary *dic = responseObject;
        
        NSMutableDictionary *infoDic = [dic objectForKey:@"tracks"];
        
        NSMutableArray *array = [infoDic objectForKey:@"list"];
        
        for (NSMutableDictionary *urlDic in array) {

            self.urlMdl = [[UrlModel alloc]init];
            
            [self.urlMdl setValuesForKeysWithDictionary:urlDic];
            
            [Player shareDataHandle].urlMdl = self.urlMdl;
            
            NSString *playUrlStr = self.urlMdl.playUrl64;
            [[Player shareDataHandle].urlArray addObject:self.urlMdl];
            [self.playUrlArray addObject:playUrlStr];
//            self.chdVC = [[CHDViewController alloc] init];
//            self.chdVC.urlMdl = self.urlMdl;
            
            [self.urlMdl release];
            
        }

        [Player Play:self.playUrlArray];
        [Player shareDataHandle].playingIndex = 0;
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [Player shareDataHandle].playingIndex = 0;
//    if ([Player shareDataHandle].player) {
//        
//        [[Player shareDataHandle].player stop];
//        [[Player shareDataHandle].player release];
//        
//    }
    if (indexPath.section == 0) {
//        if ([Player shareDataHandle].player) {
//             [[Player shareDataHandle].player stop];
//            [[Player shareDataHandle].player release];
//        }
        [self.playUrlArray removeAllObjects];
        [[Player shareDataHandle].playUrlArray removeAllObjects];

        
        RadioModels *radio = [self.radioMdlArray objectAtIndex:indexPath.row];
        
        
        [self getRadioPlayingUrl:radio];
    
    }
    
#pragma 点击跳转到另一个页面
//    WordsViewController *wordsVC = [[WordsViewController alloc]init];
//    UINavigationController *wordsC = [[UINavigationController alloc]initWithRootViewController:wordsVC];
//    
//    [self presentViewController:wordsC animated:YES completion:^{
//        
//    }];
    [self.myDelegate pushToWords];
    
    
    
    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
